package com.up.vms.interfaces.rest.utils;


import java.io.InterruptedIOException;
import java.net.UnknownHostException;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class HttpClientFactory {
    private static final Logger log = LoggerFactory.getLogger(HttpClientFactory.class);
    private static PoolingHttpClientConnectionManager cm = null;

    private HttpClientFactory() {
    }

    static CloseableHttpClient getPooledClient() {
        int timeout = 10;
        int retry = 3;
        return getPooledClient(timeout, retry);
    }

    private static CloseableHttpClient getPooledClient(int second, int retry) {
        HttpClientBuilder builder = HttpClientBuilder.create();
        if (second > 0) {
            int ms = second * 1000;
            RequestConfig config = RequestConfig.custom().setConnectTimeout(ms).setConnectionRequestTimeout(ms).setSocketTimeout(ms).build();
            builder.setDefaultRequestConfig(config);
        }

        if (retry > 0) {
            HttpRequestRetryHandler handler = (exception, executionCount, context) -> {
                if (executionCount >= retry) {
                    return false;
                } else if (exception instanceof NoHttpResponseException) {
                    return true;
                } else if (exception instanceof SSLHandshakeException) {
                    return false;
                } else if (exception instanceof InterruptedIOException) {
                    return false;
                } else if (exception instanceof UnknownHostException) {
                    return false;
                } else if (exception instanceof SSLException) {
                    return false;
                } else {
                    HttpClientContext clientContext = HttpClientContext.adapt(context);
                    HttpRequest request = clientContext.getRequest();
                    return !(request instanceof HttpEntityEnclosingRequest);
                }
            };
            builder.setRetryHandler(handler);
        }

        return builder.setConnectionManager(cm).setConnectionManagerShared(true).build();
    }

    static {
        if (cm == null) {
            try {
                ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
                LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
                Registry registry = RegistryBuilder.create().register("http", plainsf).register("https", sslsf).build();
                cm = new PoolingHttpClientConnectionManager(registry);
                cm.setMaxTotal(200);
                cm.setDefaultMaxPerRoute(50);
                IdleConnectionMonitorThread idleConnMonitor = new IdleConnectionMonitorThread(cm);
                idleConnMonitor.setName("IdleConnectionMonitorThread");
                idleConnMonitor.start();
            } catch (Exception var4) {
                log.error("" + var4.getMessage(), var4);
            }
        }

    }
}
